﻿
using Meiam.System.Common;
using Meiam.System.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Meiam.System.Interfaces
{
    public class CheckNetworkService:BaseService<DataGokinWorkshop>, ICheckNetworkService
    {
        public async Task<bool> Run(string param)
        {
            try
            {
                var machinelist = base.GetWhere(r => r.IP != null).ToList();
                List<DataGokinWorkshop> offlineMachineList = new List<DataGokinWorkshop>();

                await Task.Run(() =>
                {
                    foreach (var machine in machinelist)
                    {
                        var result = PingHeper.PingIP(machine.IP);
                        if (!result)
                        {
                            offlineMachineList.Add(machine);
                        }
                    }

                    string offlinemachineno = string.Empty;
                    offlineMachineList.ForEach(machine =>
                    {
                        if (offlineMachineList.Count >= 2)
                        {
                            offlinemachineno += $"{machine.workshop}{machine.MachineNo}({ machine.Vendor})" + ",";
                        }
                        else
                        {
                            offlinemachineno = machine.workshop + machine.MachineNo + "(" + machine.Vendor + ")";
                        }
                    });

                    if (!string.IsNullOrEmpty(offlinemachineno))
                    {
                        OAPushHelper.Push("505", "车间机台断网信息","","", param, offlinemachineno.TrimEnd(','));
                    }
                });

            }
            catch(Exception ex)
            {
                Console.WriteLine("车间网络状态检测失败,原因:" + ex.Message);
            }


            return true;

        }
    }
}
